using System;
using System.Data;
using System.Windows.Forms;
namespace mywine.Class
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CWineColor : TUnit.Class.Class
    {
        #region Declares
        int _winecolorid = -1;
        string _WineColorname = "";
        int _storeid = -1;
        #endregion

        #region Constructors
        public CWineColor()
        {
        }
        public CWineColor(TUnit.Class.CLogin Login)
        {
            base.Login = Login;
        }
        public CWineColor(TUnit.Class.CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBWineColor db = new AccDBWineColor())
            {
                this.Populate((CWineColor)db.GetClass(Login, ObjId));
            }
        }
        public CWineColor(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CWineColor(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBWineColor db = new AccDBWineColor())
            {
                this.Populate((CWineColor)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(TUnit.Data.IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("winecolorid") >= 0)
                    {
                        this._winecolorid = (dr["winecolorid"] != DBNull.Value ? Convert.ToInt32(dr["winecolorid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_WineColor ADD winecolorid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("WineColorname") >= 0)
                    {
                        this._WineColorname = (dr["WineColorname"] != DBNull.Value ? Convert.ToString(dr["WineColorname"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_WineColor ADD WineColorname [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("storeid") >= 0)
                    {
                        this._storeid = (dr["storeid"] != DBNull.Value ? Convert.ToInt32(dr["storeid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_WineColor ADD storeid [int] NULL");
                    //}
                }
                catch { }
            }
        }
        public void Populate(CWineColor Obj)
        {
            if (Obj != null)
            {
                try
                {
                    this._winecolorid = Obj.winecolorid;
                    this._WineColorname = Obj.WineColorname;
                    this._storeid = Obj.storeid;
                }
                catch { }
            }
        }
        public override bool Save()
        {
            using (TUnit.Data.IProvider p = new TUnit.Data.OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBWineColor db = new AccDBWineColor())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Save(TUnit.Data.IProvider p)
        {
            using (AccDBWineColor db = new AccDBWineColor())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override bool Delete()
        {
            using (TUnit.Data.IProvider p = new TUnit.Data.OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBWineColor db = new AccDBWineColor())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Delete(TUnit.Data.IProvider p)
        {
            using (AccDBWineColor db = new AccDBWineColor())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public string toString()
        {
            return
                this.winecolorid.ToString() + "|"
                + this.WineColorname + "";
        }

        public static string toString(TUnit.Class.CCollection Collection)
        {
            string str = "";
            if (Collection != null)
            {
                for (int i = 0; i < Collection.Count; i++)
                {
                    CWineColor o = (CWineColor)Collection[i];
                    str += o.toString();
                    if (i != (Collection.Count -1))
                        str += "@TYPE";
                }
            }
            return str;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="r"></param>
        public void Adds(CType r)
        {
            CWCTypes or = new CWCTypes(this, r);
            if(or.Save())
                this.Collection.Add(or);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="r"></param>
        public void Dels(CType r)
        {
            for (int i = 0; i < this.Collection.Count; i++)
            {
                CWCTypes or = (CWCTypes)this.Collection[i];
                if (or.typeid == r.typeid)
                {
                    if (or.Delete())
                    {
                        or.ParrentCollection.Remove(or);
                        return;
                    }
                }
            }
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CWineColor obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CWineColor WineColor);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CWineColor WineColor);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CWineColor WineColor);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CWineColor WineColor);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CWineColor obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public int winecolorid
        {
            get { return this._winecolorid; }
            set
            {
                if (this._winecolorid != value)
                {
                    this._winecolorid = value;
                }
            }
        }
        public string WineColorname
        {
            get { return this._WineColorname; }
            set
            {
                if (this._WineColorname != value)
                {
                    this._WineColorname = value;
                }
            }
        }
        public int storeid
        {
            get { return this._storeid; }
            set
            {
                if (this._storeid != value)
                {
                    this._storeid = value;
                }
            }
        }

        public TUnit.Class.CCollection _Collection = null;
        public TUnit.Class.CCollection Collection
        {
            get
            {
                if (this._Collection == null)
                {
                    using (AccDBWCTypes db = new AccDBWCTypes())
                    {
                        TUnit.Class.CCollection MyCollection = new TUnit.Class.CCollection();
                        db.GetCollection(this, MyCollection);
                        this._Collection = MyCollection;
                    }
                }
                return this._Collection;
            }
        }
        #endregion
    }

    #region Class AccDB
    public class AccDBWineColor : TUnit.Class.AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBWineColor()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(TUnit.Class.CLogin Login, ComboBox cmb, string extName)
        {
            TUnit.Data.IProvider p = new TUnit.Data.OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT winecolorid, WineColorName";
                sql += " FROM  tbl_WineColor";
                sql += " WHERE (winecolorid >= 0)";
                sql += " ORDER BY WineColorName";
                DataTable table = p.OpenTable(sql, "tbl_WineColor");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["WineColorName"] = extName;
                        row["winecolorid"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "WineColorName";
                    cmb.ValueMember = "winecolorid";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override TUnit.Class.Class GetClass(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, object ObjId)
        {
            try
            {
                string sql = "SELECT * FROM tbl_WineColor WHERE (winecolorid = " + ObjId + ")";
                DataTable table = p.OpenTable(sql, "tbl_WineColor");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return new CWineColor(Login, p, table.Rows[0]);
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(TUnit.Data.IProvider p, TUnit.Class.Class obj)
        {
            CWineColor WineColor = (CWineColor)obj;
            string sql = "SELECT * FROM tbl_WineColor";
            sql += " WHERE winecolorid =" + WineColor.winecolorid + "";
            DataTable dt = p.OpenTable(sql, "tbl_WineColor");
            if (dt != null)
            {
                DataRow row = null;
                if (dt.Rows.Count == 1 && WineColor.winecolorid != -1)
                {
                    row = dt.Rows[0];
                }
                else
                {
                    WineColor.winecolorid = Convert.ToInt32(p.InitIDKey("winecolorid", "tbl_WineColor", null));
                    row = dt.NewRow();
                    dt.Rows.Add(row);
                }
                row["winecolorid"] = WineColor.winecolorid;
                row["WineColorname"] = WineColor.WineColorname;
                row["storeid"] = WineColor.storeid;
                if (!p.UpdateTable("tbl_WineColor", dt))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(TUnit.Data.IProvider p, TUnit.Class.Class obj)
        {
            CWineColor WineColor = (CWineColor)obj;
            string sql = "DELETE FROM tbl_WineColor";
            sql += " WHERE winecolorid =" + WineColor.winecolorid + "";
            //sql +=" AND CompanyId =" + WineColor.CompanyId + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, TUnit.Class.CCollection Collection)
        {
            string sql = "SELECT * FROM tbl_WineColor ORDER BY WineColorName";
            DataTable table = p.OpenTable(sql, "tbl_WineColor");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CWineColor o = new CWineColor(Login, p, row);
                    Collection.Add(o);
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(TUnit.Data.IProvider p, TUnit.Class.Class obj, TUnit.Class.CCollection Collection)
        {
            if (obj is CStore)
            {
                string sql = "SELECT * FROM tbl_WineColor WHERE storeid ={0}";
                sql = String.Format(sql, ((CStore)obj).storeid);
                DataTable table = p.OpenTable(sql, "tbl_WineColor");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CWineColor o = new CWineColor(obj.Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        /// <param name="storeid"></param>
        /// <param name="style"></param>
        public void GetCollection(TUnit.Class.CLogin Login, TUnit.Class.CCollection Collection, int storeid, string style)
        {
            TUnit.Data.IProvider p = new TUnit.Data.OleDb(Login.Connectionstring);
            if (p.Open())
            {
                //string sql = "SELECT * FROM tbl_WineColor WHERE storeid ={0} AND style='{1}'";
                string sql = "SELECT DISTINCT tbl_types.winecolorid, tbl_types.WineColorName, tbl_wines.style";
                sql += " FROM tbl_types INNER JOIN";
                sql += " tbl_wines ON tbl_types.winecolorid = tbl_wines.locaid";
                sql += " WHERE(tbl_wines.storeid = {0}) AND (tbl_wines.style = N'{1}')";
                sql = String.Format(sql, storeid, style);
                DataTable table = p.OpenTable(sql, "tbl_WineColor");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CWineColor o = new CWineColor(Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
    }
    #endregion
}
